import { promisify } from 'util';
import { createRedLock, getLock, pool, RedisClient } from '../db/redis';

export default function (target: any) {
  target.prototype.getRedisClient = (): Promise<RedisClient> => {
    return new Promise((resolve) => {
      pool.acquire().then((client: RedisClient | any) => {
        const redLock = createRedLock(client);
        client.getLock = getLock.bind(client, redLock);
        client.release = pool.release.bind(pool, client);
        client._set = promisify(client.set).bind(client);
        client._hset = promisify(client.hset).bind(client);
        client._hvals = promisify(client.hvals).bind(client);
        client._hget = promisify(client.hget).bind(client);
        resolve(client);
      });
    });
  };
}
